package com.dreamer.view.pyramid;

import com.dreamer.domain.pyramid.ParamConfig;
import com.dreamer.domain.pyramid.Pyramid;
import com.dreamer.domain.user.User;
import com.dreamer.service.pyramid.ParamConfigHandler;
import com.dreamer.service.pyramid.PyramidHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import ps.mx.otter.utils.WebUtil;
import ps.mx.otter.utils.message.Message;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Optional;

@RestController
@RequestMapping("/paramConfig")
public class ParamCongifController {

    @Autowired
    private ParamConfigHandler paramConfigHandler;

    @RequestMapping(value = "/edit.json", method = RequestMethod.POST)
    public Message edit(@ModelAttribute("parameter") ParamConfig paramConfig) {
        paramConfig.setUpdateTime(new Date());
        ParamConfig pyramid1 = paramConfigHandler.saveOrupdate(paramConfig);
        if(pyramid1 != null){
            return Message.createSuccessMessage();
        }else{
            return Message.createFailedMessage();
        }
    }

    @ModelAttribute("parameter")
    public ParamConfig preprocess(@RequestParam("id") Optional<Integer> id) {
        ParamConfig paramConfig;
        if (id.isPresent()) {
            paramConfig = paramConfigHandler.findById(id.get());
        }else{
            paramConfig = new ParamConfig();
        }
        return paramConfig;
    }

}
